package cn.tedu.tea.service;

import cn.tedu.tea.domain.Content;
import cn.tedu.tea.domain.ContentDetail;
import cn.tedu.tea.domain.param.ContentAddNewDTO;
import cn.tedu.tea.domain.param.ContentUpdateDTO;
import cn.tedu.tea.domain.vo.ContentListVO;
import cn.tedu.tea.domain.vo.ContentStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.ContentMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ContentServiceImpl implements IContentService {

    @Autowired
    private ContentMapper contentMapper;

    @Override
    public void addNew(ContentAddNewDTO contentAddNewDTO) {
        log.debug("开始处理【添加内容】的业务，参数： {}", contentAddNewDTO);
        int count = contentMapper.countByTitle(contentAddNewDTO.getTitle());
        if (count > 0){
            String message = "添加内容失败，标题已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Content content = new Content();
        BeanUtils.copyProperties(contentAddNewDTO, content);
        content.setUp(0).setDown(0).setAuditStatus(0).setClickTimes(0).setCommentTimes(0).setModifiedUserId(0L).setModifiedUserName("");
        int rows = contentMapper.insert(content);
        if (rows != 1){
            String message = "添加内容失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
        ContentDetail contentDetail = new ContentDetail();
        contentDetail.setDetail(contentAddNewDTO.getDetail());
        contentDetail.setModifiedUserId(contentAddNewDTO.getCreateUserId());
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除内容】的业务，参数： {}", id);
        ContentStandardVO contentStandard = contentMapper.getStandardById(id);
        if (contentStandard == null){
            String message = "删除内容失败，要删除的内容不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = contentMapper.deleteById(id);
        if (rows != 1){
            String message = "删除内容失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除内容】的业务，参数： {}", ids);
        int rows = contentMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除内容失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateContentInfoById(ContentUpdateDTO contentUpdateDTO) {
        log.debug("开始处理【修改内容信息】的业务，参数：{}", contentUpdateDTO);
        ContentStandardVO contentStandard = contentMapper.getStandardById(contentUpdateDTO.getId());
        if (contentStandard == null){
            String message = "修改内容失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = contentMapper.countByTitleAndNotId(contentUpdateDTO.getTitle(), contentUpdateDTO.getId());
        if (count > 0){
            String message = "修改内容失败，标题已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Content content = new Content();
        BeanUtils.copyProperties(contentUpdateDTO, content);
        int rows = contentMapper.update(content);
        if (rows != 1){
            String message = "修改内容失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将内容设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将内容设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public ContentStandardVO getContentDetailStandardById(Integer id) {
        log.debug("开始处理【根据id查询内容详情】的业务，参数： {}", id);
        ContentStandardVO contentStandard = contentMapper.getStandardById(id);
        if (contentStandard == null){
            String message = "查询内容详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return contentStandard;
    }

    @Override
    public List<ContentListVO> list() {
        log.debug("开始处理【查询内容列表】的业务，无参数");
        List<ContentListVO> list = contentMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        ContentStandardVO currentContent = contentMapper.getStandardById(id);
        if (currentContent == null) {
            String message = STATUS_TEXT[status] + "内容失败，内容数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentContent.getStatus() == status) {
            String message = STATUS_TEXT[status] + "内容失败，此内容已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Content updateContent = new Content();
        updateContent.setId(id);
        updateContent.setStatus(status);
        int rows = contentMapper.update(updateContent);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "内容失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
